package com.yangyang.androidconnect.phoneinfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.alibaba.fastjson.JSON;
import com.yangyang.androidconnect.phoneinfo.model.CallRecord;
import com.yangyang.androidconnect.phoneinfo.model.Contact;
import com.yangyang.androidconnect.phoneinfo.model.SMSRecord;

import java.util.List;

// DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "PhoneData.db";
    private static final int DATABASE_VERSION = 1;

    // 联系人表
    private static final String TABLE_CONTACTS = "contacts";
    private static final String CREATE_TABLE_CONTACTS =
            "CREATE TABLE " + TABLE_CONTACTS + "(" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "contact_id TEXT," +
                    "name TEXT," +
                    "phone_numbers TEXT," + // JSON格式存储多个号码
                    "timestamp INTEGER" +
                    ")";

    // 通话记录表
    private static final String TABLE_CALL_LOGS = "call_logs";
    private static final String CREATE_TABLE_CALL_LOGS =
            "CREATE TABLE " + TABLE_CALL_LOGS + "(" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "number TEXT," +
                    "call_date INTEGER," +
                    "duration INTEGER," +
                    "type INTEGER," +
                    "timestamp INTEGER" +
                    ")";

    // 短信记录表
    private static final String TABLE_SMS = "sms_records";
    private static final String CREATE_TABLE_SMS =
            "CREATE TABLE " + TABLE_SMS + "(" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "address TEXT," +
                    "body TEXT," +
                    "sms_date INTEGER," +
                    "type INTEGER," +
                    "timestamp INTEGER" +
                    ")";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_CONTACTS);
        db.execSQL(CREATE_TABLE_CALL_LOGS);
        db.execSQL(CREATE_TABLE_SMS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CALL_LOGS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SMS);
        onCreate(db);
    }

    // 保存联系人到数据库
    public void saveContacts(List<Contact> contacts) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();

        try {
            // 清空旧数据
            db.delete(TABLE_CONTACTS, null, null);

            for (Contact contact : contacts) {
                ContentValues values = new ContentValues();
                values.put("contact_id", contact.getId());
                values.put("name", contact.getName());

                // 将手机号码列表转为JSON存储
                String phoneNumbersJson = JSON.toJSONString(contact.getPhoneNumbers());

                values.put("phone_numbers", phoneNumbersJson);
                values.put("timestamp", contact.getTimestamp());

                db.insert(TABLE_CONTACTS, null, values);
            }

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    // 保存通话记录到数据库
    public void saveCallLogs(List<CallRecord> callRecords) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();

        try {
            db.delete(TABLE_CALL_LOGS, null, null);

            for (CallRecord record : callRecords) {
                ContentValues values = new ContentValues();
                values.put("number", record.getNumber());
                values.put("call_date", record.getDate());
                values.put("duration", record.getDuration());
                values.put("type", record.getType());
                values.put("timestamp", record.getTimestamp());

                db.insert(TABLE_CALL_LOGS, null, values);
            }

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    // 保存短信记录到数据库
    public void saveSMSRecords(List<SMSRecord> smsRecords) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();

        try {
            db.delete(TABLE_SMS, null, null);

            for (SMSRecord record : smsRecords) {
                ContentValues values = new ContentValues();
                values.put("address", record.getAddress());
                values.put("body", record.getBody());
                values.put("sms_date", record.getDate());
                values.put("type", record.getType());
                values.put("timestamp", record.getTimestamp());

                db.insert(TABLE_SMS, null, values);
            }

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}